local assets = {
	Asset("ANIM", "anim/treedrake.zip"),
	Asset("ANIM", "anim/musha_moondrake_blue.zip"),
	Asset("ANIM", "anim/musha_moondrake_yellow.zip")
}

local prefabs = {}

local brain = require("brains/musha_moonnutdrakebrain")

local function Retarget(inst)
	return FindEntity(
		inst,
		TUNING.TENTACLE_ATTACK_DIST,
		function(guy)
			return guy.prefab ~= inst.prefab and guy.entity:IsVisible() and not guy.components.health:IsDead() and
				guy ~= (inst or inst.components.follower.leader) and
				not (not guy.components.follower or
					(guy.components.follower.leader and guy.components.follower.leader == inst.components.follower.leader)) and
				not guy:HasTag("player") and
				not guy:HasTag("companion") and
				not guy:HasTag("wall")
		end,
		{"_combat", "_health"},
		{"prey"}
	)
end

local function KeepTargetFn(inst, target)
	return not inst.sg:HasStateTag("exit") and
		(inst.sg:HasStateTag("hidden") or
			(target ~= nil and not target.components.health:IsDead() and inst.components.combat:CanTarget(target) and
				inst:IsNear(target, 20)))
end

local function CanShareTarget(dude)
	return (dude:HasTag("moondrake")) and not dude.components.health:IsDead()
end

local function OnHitfreeze(inst, data)
	local other = data.target
	if not (other:HasTag("smashable")) then
		if other and other.components.freezable then
			--other.components.freezable:SpawnShatterFX()
			other.components.freezable:AddColdness(0.25)
		elseif other and other:HasTag("_musha") then
			inst.components.combat:SetTarget(nil)
			inst.components.combat:GiveUp()
		end
		if other.components.burnable and other.components.burnable:IsBurning() then
			other.components.burnable:Extinguish()
		end
	end
end

local function OnAttacked(inst, data)
	if data.attacker and data.attacker:HasTag("_musha") or data.attacker:HasTag("player") then
		inst.components.combat:SetTarget(nil)
		inst.components.combat:GiveUp()
	end
	inst.components.combat:SetTarget(data.attacker)
	inst.components.combat:ShareTarget(data.attacker, 15, CanShareTarget, 10)
end

local function OnLostTarget(inst)
	if not inst.sg:HasStateTag("hidden") and inst:GetTimeAlive() > 5 then
		inst.sg:GoToState("exit")
	end
end

local function OnDeath(inst, data, immediate)
	if inst.components.burnable ~= nil and inst.components.burnable:IsBurning() then
		if immediate then
			inst.components.burnable:Extinguish()
		else
			inst:DoTaskInTime(.5, OnDeath, nil, true)
		end
	end
end

local function fn()
	local inst = CreateEntity()

	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	inst.entity:AddDynamicShadow()
	inst.entity:AddNetwork()

	inst.DynamicShadow:SetSize(1.25, .75)

	inst.Transform:SetFourFaced()
	MakeCharacterPhysics(inst, 1, .25)

	inst.AnimState:SetBank("treedrake")
	inst.AnimState:SetBuild(RandomChoice({"musha_moondrake_yellow", "musha_moondrake_blue"}))
	inst.AnimState:PlayAnimation("enter")

	--  inst:AddTag("beaverchewable")
	--  inst:AddTag("birchnutdrake")
	-- inst:AddTag("monster")
	inst:AddTag("companion")
	inst:AddTag("moondrake")
	inst:AddTag("scarytoprey")
	inst:AddTag("NOBLOCK")
	if math.random() < 0.2 then
		inst.Transform:SetScale(0.65, 0.65, 0.65)
	elseif math.random() < 0.2 then
		inst.Transform:SetScale(1.2, 1.2, 1.2)
	elseif math.random() < 0.2 then
		inst.Transform:SetScale(0.8, 0.8, 0.8)
	elseif math.random() < 0.2 then
		inst.Transform:SetScale(0.7, 0.7, 0.7)
	elseif math.random() < 0.2 then
		inst.Transform:SetScale(0.55, 0.55, 0.55)
	elseif math.random() < 0.2 then
		inst.Transform:SetScale(1, 1, 1)
	else
		inst.Transform:SetScale(0.9, 0.9, 0.9)
	end

	inst.entity:SetPristine()

	if not TheWorld.ismastersim then
		return inst
	end

	inst:AddComponent("inspectable")

	inst:AddComponent("lootdropper")
	inst.components.lootdropper.numrandomloot = 1
	-- inst.components.lootdropper:AddRandomLoot("acorn", .1)

	inst:AddComponent("locomotor")
	inst.components.locomotor.walkspeed = 3.5

	inst:AddComponent("combat")
	inst.components.combat:SetDefaultDamage(5)
	inst.components.combat:SetRange(2.5, 3)
	inst.components.combat:SetAttackPeriod(2)
	inst.components.combat:SetRetargetFunction(1, Retarget)
	inst.components.combat:SetKeepTargetFunction(KeepTargetFn)
	--inst.components.combat:SetHurtSound("dontstarve_DLC001/creatures/decidous/drake_hit")
	inst.components.combat:SetHurtSound("dontstarve_DLC001/creatures/deciduous/drake_hit")
	inst:ListenForEvent("attacked", OnAttacked)
	inst:ListenForEvent("onhitother", OnHitfreeze)
	inst.components.combat.playerdamagepercent = 0
	inst:DoTaskInTime(5, inst.ListenForEvent, "losttarget", OnLostTarget)

	inst:AddComponent("health")
	inst.components.health:SetMaxHealth(50)

	inst:AddComponent("follower")
	inst.components.follower:KeepLeaderOnAttacked()
	inst.components.follower.keepdeadleader = true

	inst:AddComponent("sleeper")

	--inst:SetStateGraph("SGmoonnutdrake")
	inst:SetStateGraph("SGbirchnutdrake")
	inst:SetBrain(brain)

	MakeSmallBurnableCharacter(inst, "treedrake_root", Vector3(0, -1, .1))

	inst.components.burnable:SetBurnTime(10)
	inst.components.health.fire_damage_scale = 2
	inst:ListenForEvent("death", OnDeath)
	inst.components.propagator.flashpoint = 5 + math.random() * 3
	MakeSmallFreezableCharacter(inst)
	inst:DoTaskInTime(
		10,
		function()
			inst.slave = false
		end
	)

	return inst
end

return Prefab("musha_moondrake", fn, assets, prefabs)
